﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OleDb;
using Com.Toook.Common;

public partial class nst_joke_JokeEditPublish : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            InitTypeData();
            InitJoke();
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string sql = "update PublishJoke set title=@title, content=@content, author=@author, score=@score " +
            " where typePageId=" + lblId.Text;
        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        cmd.Parameters.Add("@title", OleDbType.VarWChar, 30).Value = txtTitle.Text;
        cmd.Parameters.Add("@content", OleDbType.LongVarWChar).Value = txtContent.Text;
        cmd.Parameters.Add("@author", OleDbType.VarWChar, 50).Value = txtAuthor.Text;
        cmd.Parameters.Add("@score", OleDbType.Integer).Value = ddlScore.SelectedValue;

        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            lblMsg.Text = "修改成功";
        }
        catch(Exception ex)
        {
            LogHelper.Log.Error(ex);
            lblMsg.Text = "修改失败" + ex.Message;
        }
        finally
        {
            con.Close();
        }
    }
    private void InitJoke()
    {
        string typePageId = Request["id"];
        int id = 0;
        if (string.IsNullOrEmpty(typePageId) || !int.TryParse(typePageId, out id))
        {
            return;
        }
        string type = "51";
        string score = "5";
        string sql = "select id, title, author, content, score " + //, clickCount, likeCount
            " from PublishJoke where typePageId=" + typePageId;
        OleDbConnection con = AccessHelper.GetConnection();
        OleDbCommand cmd = new OleDbCommand(sql, con);
        OleDbDataReader reader = null;
        try
        {
            con.Open();
            reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                lblId.Text = Request["id"];
                txtContent.Text = reader["content"].ToString();
                txtTitle.Text = reader["title"].ToString();
                txtContent.Text = reader["content"].ToString();

                score = reader["score"].ToString();
                ParamHelper.SetDropDownListSelectedIndex(ddlScore, score);
                type = typePageId.Substring(0, 2);
                ParamHelper.SetDropDownListSelectedIndex(ddlType, type);
            }
            else
            {
                lblMsg.Text = "没有读取到数据 typePageId=" + typePageId;
            }
        }
        catch(Exception ex)
        {
            lblMsg.Text = "读取出错 typePageId=" + typePageId + ex.Message;
            LogHelper.Log.Error(ex);
        }
        finally
        {
            AccessHelper.CloseDbResource(con, reader);
        }        
    }
    private void InitTypeData()
    {
        DataRow[] rows = PageData.SelectParamRows("parentId=50");
        foreach (DataRow row in rows)
        {
            ddlType.Items.Add(new ListItem(row["typeName"].ToString(), row["typeId"].ToString()));
        }
    }
}
